package dao;

import java.lang.reflect.ParameterizedType;

import org.hibernate.Session;


public abstract class AbstractDAO<T> {
	
	public void create(T a)
	{
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.save(a);
        session.getTransaction().commit();
	}
	
	public T retrieve(long id)
	{
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        T a = (T)session.get(getModel(), id);
        session.getTransaction().commit();
        return a;
	}
	
	public void update(T a)
	{
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.save(a);
        session.getTransaction().commit();
	}
	
	public void delete(T a)
	{
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.delete(a);
        session.getTransaction().commit();
	}
	
	//Personne n'a rien vu !
    public Class<T> getModel()
    {
        ParameterizedType superclass = (ParameterizedType)getClass().getGenericSuperclass();
        return (Class<T>) superclass.getActualTypeArguments()[0];
	}
}
